package com.asdf.application.commands.dialogHandlers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import com.asdf.external.INetserviceSimulator;
import com.asdf.ui.ConnectDialog;
import com.asdf.ui.SubscribeDialog;

/**
 * @author Artem Barger
 * 
 * Handler for actions where there is a need to proceed the user insertion
 *
 */
public class DialogHandlerNewUser implements IDialogHandler, ActionListener
{

	private static final String	SUBSCRIBE_DLG_CANCEL	= "SUBSCRIBE_DLG_CANCEL";
	private static final String	SUBSCRIBE_DLG_SUBSCRIBE	= "SUBSCRIBE_DLG_SUBSCRIBE";

	// TODO: There is a bug here!
	private SubscribeDialog dlgSubscribe;
	private ConnectDialog	dlg;

	private Map< String, IDialogHandler> handlers = new HashMap< String, IDialogHandler>( );
	private INetserviceSimulator	simulator;
	
	/**
	 * @param dlg reference to the window dialog which attached to this
	 * handler.
	 */
	public DialogHandlerNewUser( ConnectDialog dlg)
	{
		this.dlg = dlg;
	}

	@Override
	public void handle( INetserviceSimulator simulator)
	{
		this.simulator = simulator;
		dlg.dispose( );
		dlgSubscribe = new SubscribeDialog( );
		handlers.put( SUBSCRIBE_DLG_CANCEL, new DialogHandlerCancel( dlgSubscribe));
		dlgSubscribe.registerEventHandlerCancelCommand( SUBSCRIBE_DLG_CANCEL, this);
		
		handlers.put( SUBSCRIBE_DLG_SUBSCRIBE, new DialogHandlerSubscribeUser( dlgSubscribe));
		dlgSubscribe.registerEventHandlerSubscribeCommand( SUBSCRIBE_DLG_SUBSCRIBE, this);

		dlgSubscribe.setVisible( true);
	}

	@Override
	public void actionPerformed( ActionEvent arg0)
	{
		if ( handlers.containsKey( arg0.getActionCommand( )))
				handlers.get( arg0.getActionCommand( )).handle( simulator);
	}

}
